using System;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Web.UI;
namespace PmfWeb.Core.WebControls
{
    ///<summary>
    ///</summary>
    public class AttributeCollection
    {
        private string _AttrColl;

        ///<summary>
        ///</summary>
        public AttributeCollection()
        {
            this._AttrColl = "";
        }

        ///<summary>
        ///</summary>
        ///<param name="attributes"></param>
        public AttributeCollection(string attributes)
        {
            if (attributes == null)
            {
                this._AttrColl = "";
            }
            else
            {
                this._AttrColl = attributes;
            }
        }

        ///<summary>
        ///</summary>
        ///<param name="name"></param>
        ///<param name="value"></param>
        public void Add(string name, string value)
        {
            if ((value != null) && (value != ""))
            {
                string str = " " + name.ToLower() + "='";
                string str2 = value.Replace("'", "\"");
                if (this._AttrColl.IndexOf(str) != -1)
                {
                    switch (str)
                    {
                        case " style='":
                            this._AttrColl = this._AttrColl.Replace(str, str + str2 + "; ");
                            return;

                        case " class='":
                            this._AttrColl = this._AttrColl.Replace(str, str + str2 + " ");
                            return;
                    }
                    this.Remove(name);
                }
                this._AttrColl = this._AttrColl + (str + str2 + "'");
            }
        }

        ///<summary>
        ///</summary>
        public void Clear()
        {
            this._AttrColl = "";
        }

        private static Regex CreateRegex(string name)
        {
            return new Regex("(^.*) " + name + "='([^']*)'(.*$)", RegexOptions.Multiline | RegexOptions.IgnoreCase);
        }

        ///<summary>
        ///</summary>
        ///<param name="name"></param>
        ///<returns></returns>
        public string Get(string name)
        {
            Regex regex = CreateRegex(name);
            if (!regex.IsMatch(this._AttrColl))
            {
                return "";
            }
            return regex.Replace(this._AttrColl, "$2");
        }

        ///<summary>
        ///</summary>
        ///<param name="name"></param>
        public void Remove(string name)
        {
            Regex regex = CreateRegex(name);
            if (regex.IsMatch(this._AttrColl))
            {
                this._AttrColl = regex.Replace(this._AttrColl, "$1$3");
            }
        }

        ///<summary>
        ///</summary>
        ///<param name="output"></param>
        public virtual void Render(HtmlTextWriter output)
        {
            output.Write(this._AttrColl);
        }

        ///<summary>
        ///</summary>
        ///<returns></returns>
        public override string ToString()
        {
            return this._AttrColl;
        }

        public string this[string index]
        {
            get
            {
                return this.Get(index);
            }
            set
            {
                this.Add(index, value);
            }
        }
    }
}

